import React from 'react'
import './HomeGrid.css'
import Swiper from '../../../../components/Swiper/swiper'
import  {Toast} from  'antd-mobile'
import  {AjaxSend} from "../../../../util/util";
import sign_1 from  './sign_1.png'
import sign_2 from  './sign_2.png'
import sign_3 from  './sign_3.png'
import  closeIcon from  './close.png'
class HomeGrid extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            issingshow:false,
            HomeTop_bg:1,
            sign_count:0,
            sign_ad_list:'',
            active:'',
            cumulative_active:'',
            partner_bonus_cash:'',
            cumulative_partner_bonus:'',
            integral:'',
            sum_integral:'',
            product_href:'productinfo.php?product_id=',
            user_code:this.props.user_code
        }
        this.sign_ad_list = this.sign_ad_list.bind(this);
        this.CloseSignClick = this.CloseSignClick.bind(this)
    }
    componentDidMount() {
        this.sign_count()

        this.get_userinfo()
    }
    CloseSignClick(){
        this.setState({
            issingshow :false
        })
    }
    handleClick(index,item){
        switch(item.class_id)
        {
            case '109':
                if(this.state.sign_count>=1){
                    Toast.info('今日已签到', 2);
                }else {
                    this.sign_ad_list()
                    this.setState({
                        issingshow :true
                    })
                }
                break;
            case '1000091':
                Toast.info('功能正在开发中', 2);
                break;
            case '1000088':
                window.location.href='my_code_new.php?key='+this.props.user_code
                break;
            default:
                window.location.href=item.web_url;
        }
    }
    sign_count(){
        const data={
            _cmd: 'usercenter_sign_count'
        }
        AjaxSend(data, (da) =>  {
            if (da.data.count >= 1) {
                console.log('已签到');
            }
            this.setState({
                sign_count:da.data.count,
            })
        }, (errors)  => {
            alert(errors);
        })
    }
    sign_ad_list(){
        const data={
            _cmd: 'usercenter_sign_submit'
        }
        AjaxSend(data, (da) =>  {
            this.setState({
                sign_ad_list:da.data.product_ad,
                active:da.data.active,
                cumulative_active:da.data.cumulative_active,
                partner_bonus_cash:da.data.partner_bonus_cash,
                cumulative_partner_bonus:da.data.cumulative_partner_bonus,
                integral:da.data.integral,
                sum_integral:da.data.sum_integral
            })
        }, (errors)  => {
            alert(errors);
        })
    }
    get_userinfo(){
        // usercenter_index
        const data={
            _cmd: 'usercenter_index'
        }
        AjaxSend(data, (da) =>  {
            console.log(JSON.stringify(da.data))
        }, (errors)  => {
            alert(errors);
        })
    }
    go_to_product_info(product_id){
        window.location.href=this.state.product_href+product_id
    }
    render() {
        return (
            <div
                style={{
                    overflow:'hidden',
                    margin:'5px 0 5px 0'
                }}
            >
                {this.props.HomeTopClassData.map((item,index) =>{
                    return (<div className="HomeGrid_Item" key={item}
                                 onClick={()=> this.handleClick(index,item)} >
                        <img src={item.image_url}/>
                        {item.class_name}
                    </div>)
                })}
                {this.state.issingshow?
                    <div className="rlayer">
                        <div className="sign_award">
                            <div className="sign_awerd_top"
                                 style={{
                                     backgroundImage: 'url(' + sign_1 + ')',
                                     backgroundSize: 'cover'
                                 }}
                            >
                                <div className="sign_close_btn"
                                  onClick={()=>(this.CloseSignClick())}
                                 ><img src={closeIcon}/></div>
                                <div className="sign_add_integral">积分+1</div>
                                <div className="sign_sum_integral">积分余额{this.state.sum_integral}</div>
                                <div className="rlayer_sign_ad">
                                    {this.state.sign_ad_list ? <div className="rlayer_sign_ad_list">
                                            {this.state.sign_ad_list.map(v => (
                                                <div className="rlayer_sign_ad_list_item"
                                                     onClick={ () => {
                                                         this.go_to_product_info(v.product_id)
                                                     } }
                                                >
                                                    <img src={v.image_url}/>
                                                    <p>{v.product_name}</p>
                                                </div>
                                            ))}
                                        </div> : '<h1>54321</h1>'}

                                </div>
                            </div>
                            {this.state.active ?
                                <div className="sign_awerd_content_1"
                                     style={{
                                         backgroundImage: 'url(' + sign_2 + ')',
                                         backgroundSize: 'cover'
                                     }}
                                >
                                    <div className="sign_awerd_num">
                                        <p>今日签到累计获得￥ {this.state.active}</p>
                                        <p className="sign_awerd_num_small">已累计获得￥ {this.state.cumulative_active}</p>
                                    </div>
                                </div>
                                : ''
                            }
                            {this.state.partner_bonus_cash ?
                                <div className="sign_awerd_content_2"
                                     style={{
                                         backgroundImage: 'url(' + sign_3 + ')',
                                         backgroundSize: 'cover'
                                     }}
                                >
                                    <div className="sign_awerd_num">
                                        <p>今日签到累计获得￥ {this.state.partner_bonus_cash}</p>
                                        <p className="sign_awerd_num_small">
                                            已累计获得￥ {this.state.cumulative_partner_bonus}</p>
                                    </div>
                                </div>
                                : ''
                            }

                        </div>
                    </div>:''
                }
            </div>
        )
    }

}

export default HomeGrid